<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/10 0010
 * Time: 17:03
 */

namespace api\storage\controller;

use api\storage\common\Constant;
use api\storage\service\MachineServer;
use service\file\CommonFile;
use think\Request;

class MachineController extends AppBaseController
{
    protected $machineServer;
    protected $file;
    protected $storageUuid;

    /**
     * MachineController constructor.
     * @param Request|null $request
     * @param MachineServer $machineServer
     */
    public function __construct(Request $request = null, MachineServer $machineServer)
    {
        parent::__construct($request);
        $this->machineServer = $machineServer;
    }


    /**
     * @api {get} /api/storage/machine/get_reel_destination 1.获取站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName get_reel_destination
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription 扫码获取当前物料要入站的站位
     *
     * @apiParam {string}  reelId  盘料上的reelId
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 是否上料？
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 该机器正在生产
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 该物料是退料物料
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 该物料是切枪物料
     *
     *
     **/
    public function getDestination()
    {
        $reelId = input('reelId');
        $result = $this->machineServer->getReelDestination($reelId, $this->getStorageUuid());
        switch ($result['status']) {
            case 1:
                $this->error("该盘料不是准备上料的物料");
                break;
            case 2:
                $this->success("是否上料？", $result['data']);
                break;
            case 3:
                $this->error("该机器正在生产");
                break;
            case 4:
                $this->error('该物料是退料物料');
                break;
            case 5:
                $this->error("该物料是切枪物料");
                break;
        }
    }

    /**
     * @api {put} /api/storage/machine/put_machine 2.物料上机
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName put_machine
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription 将物料放入对应的机器站位上
     *
     * @apiParam {string}  reelId  盘料上的reelId
     * *@apiParam {string}  machineId  机器Id
     * @apiParam {string}  seatCode  站位号
     * @apiParam {string}  feedId  工单号
     * 对扫描的物料进行出库
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 上料成功
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 上料失败
     **/
    public function putMachine()
    {
        $machineId = input('machineId');
        $seatCode = input('seatCode');
        $reelId = input('reelId');
        $feedId = input('feedId');
        $result = $this->machineServer->putMachine($feedId,$machineId,$seatCode,$reelId, $this->getStorageUuid());
        switch ($result['status']) {
            case 1:
                $this->error("该盘料不是准备上料的物料");
                break;
            case 2:
                $this->success("上料成功");
                break;
            case 3:
                $this->error("该机器正在生产");
                break;

        }

    }
    /**
     * @api {get} /api/storage/machine/enter_add_materiel 3.检测是否有未完成续料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName enter_add_materiel
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription 确认是否有未完成的续料,如果有则继续未完成续料
     *
     * 检查扫描物料是否需要续料
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 之前续料未完成
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 正常进入续料
     **/
public function enterAddMateriel(){
    $result = $this->machineServer->enterAddMateriel($this->getUsername(),$this->getStorageUuid());
    switch ($result['status']){
        case 1:
            $this->success("之前续料未完成",$result['data']);
            break;
        case 2:
            $this->error( "正常进入续料");
            break;
    }
}
    /**
     * @api {get} /api/storage/machine/add_materiel_check 4.续料盘料检查
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName add_materiel_check
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription 检查扫描物料是否需要续料
     *
     * @apiParam {string}  reelId  盘料上的reelId
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 该物料未发满，进入续料流程
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 该物料并非机器上生产的物料
     * @apiError   {Error} code 261
     * @apiError   {Error}msg 该物料已经发满，是否继续续料？
     **/
    public function addMaterielCheck(){
        $reel = input('reelId');
        $result = $this->machineServer->addMaterielCheck($reel,$this->getUsername(),$this->getStorageUuid());
        switch ($result['status']){
            case 1:
                $this->success("该物料未发满，进入续料流程",$result['data']);
                break;
            case 2:
                $this->error("该物料已经发满，是否继续续料？",$result['data'],Constant::ADD_MATERIEL_CODE_IS_FULL);
                break;
            case 3:
                $this->error("该物料并非机器上生产的物料");
                break;
            case 4:
                $this->error("该物料被锁定,可能正在续料或者被剪料");
                break;
            case 5:
                $this->error("有未完成流程",$result['data'] ,Constant::ADD_MATERIEL_IS_CONTINUE);
        }
    }
    /**
     * @api {put} /api/storage/machine/add_materiel 5.生成续料文件
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName add_materiel
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription 生成续料出库时需要用到的文件
     * @apiParam {string}  feedDetailUuid  上一个接口的返回值,指的是申请续料的的feedDetail的Uuid
     * @apiParam {string}  isEnough  是否已经发够
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 机器剪料
     * @apiSuccess {Array} data 返回续料单数据
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 货架取料
     * @apiSuccess {Array} data 返回续料单数据
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 未找到可用物料
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 当前物料已发够，无法再发
     **/
    public function addMateriel(){
        $feedDetailUuid = input("feedDetailUuid");
        $isEnough = input("isEnough");//若为1说明已经发够了，仍旧续料。若为0说明没发够情况下的正常续料
        $result = $this->machineServer->addMateriel($feedDetailUuid,$isEnough,$this->getUsername(),$this->getStorageUuid());
        switch ($result['status']){
            case 1:
                $this->error('未找到可用物料，可能该物料未上料或未归还到库中，请先将他们进行上料或入库后再重试。');
                break;
            case 2:
                $this->success("机器剪料",$result['data']);
                break;
            case 3:
                $this->success("货架取料",$result['data']);
                break;
            case 4:
                $this->error('当前物料已发够，无法再发');
                break;
            default:
                $this->error('addMateriel方法出现错误');
                break;
        }
    }

    /**
     * @api {post} /api/storage/machine/add_materiel_output 6.续料出库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName add_materiel_output
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription 对续料进行出库
     *
     * @apiParam {string}  feedDetailUuid  上一个接口的返回值 指的是申请续料的的feedDetail的Uuid
     * @apiParam {int}  outputNum  剪取物料的数量
     * @apiParam {int}  fromShelves  如果是从货架出库则设置为1，否则设置为0
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 机器剪料
     * @apiSuccess {Array} data 返回续料单数据
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 货架取料
     * @apiSuccess {Array} data 返回续料单数据
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 未找到可用物料
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 当前物料已发够，无法再发
     **/

    public function addMaterielOutput()
    {
        $feedDetailUuid = input('feedDetailUuid');
        $outputNum = input('outputNum');
        $fromShelves = input('fromShelves');//如果为1则是货架出库，如为0则是机器续料
        $result = $this->machineServer->addMaterielOutput($feedDetailUuid, $outputNum, $this->getUsername(),$fromShelves, $this->getStorageUuid());
        switch ($result['status']) {
            case 1:
                $this->success("出库成功", $result['data']);
                break;
            case 2:
                $this->error("该reel不存在仓库中", $result['data']);
                break;
            case 3:
                $this->success("剪料成功",$result['data']);
                break;
        }
    }

    /**
     * @api {delete} /api/storage/machine/add_materiel_over 7.结束续料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName add_materiel_over
     * @apiGroup  machine
     * @apiVersion 0.0.1
     * @apiDescription  点击以后会根据用户id和仓库删除续料文件，也就是feedAddMateriel
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 剪料结束成功
     **/
    public function addMaterielOver(){

        $result = $this->machineServer->addMaterielOver($this->getUsername(), $this->getStorageUuid());
        switch ($result['status']){
            case 1:
                $this->error("结束失败，机器续料未结束");
                break;
            case 2:
                $this->error("结束失败，货架续料未结束");
                break;
            case 3:
                $this->error("此时没有续料？");
                break;
            case 4:
                $this->success('结束成功');
                break;
            case 5:
                $this->error("结束失败，机器续料和货架续料都未结束");
        }

    }

    /****
     * @param $name
     * @param $storageUuid
     * 实例化续料对应的文件
     */
    public function materielFile($name, $storageUuid)
    {
        $this->storageUuid = $storageUuid;
        $filePath = Constant::RECORD_FEED_ADD_MATERIEL_POSITION . $name . Constant::RECORD_FILE_SUFFIX;
        $this->file = new CommonFile($storageUuid, $filePath);

    }
}
